﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;


namespace Mole
{
    [Serializable]
    public class UrlSession
    {
        public BindingList<UrlProcessed> UrlComplete { get; set; }
        public BindingList<DirectoryList> DirectoryListUrl { get; set; }
        public BindingList<DirectoryList> DirectoryListAll { get; set; }
        public string ProjectName { get; set; }
        public List<String> UrlTargets { get; set; }
        public List<UrlProcessing> UrlProcessing {get; set;}
        public String AppDir { get; set; }
        public uint SiteId { get; set; }

        public UrlSession()
        {
            //URLComplete = new BindingList<UrlProcessed>();
            //DirectoryListURL = new BindingList<DirectoryList>();
            //DirectoryListALL = new BindingList<DirectoryList>();
            //URLProcessing = new List<UrlProcessing>();
            //ProjectName = String.Empty;
            //URLTargets = new List<string>();
            //AppDir = Application.StartupPath;
            //Siteid = 0;
        }

        public void SetUrlSession(string project, List<UrlCheck> urls)
        {
            UrlComplete = new BindingList<UrlProcessed>();
            DirectoryListUrl = new BindingList<DirectoryList>();
            DirectoryListAll = new BindingList<DirectoryList>();
            UrlProcessing = new List<UrlProcessing>();
            ProjectName = project;
            UrlTargets = new List<string>();
            AppDir = Application.StartupPath;
            SiteId = 0;

            SetUrlTargets(urls);
        }

        public void SetUrlSession(string project, string urls)
        {
            UrlComplete = new BindingList<UrlProcessed>();
            DirectoryListUrl = new BindingList<DirectoryList>();
            DirectoryListAll = new BindingList<DirectoryList>();
            UrlProcessing = new List<UrlProcessing>();
            ProjectName = project;
            UrlTargets = new List<string>();
            AppDir = Application.StartupPath;
            SiteId = 0;

            SetUrlTargets(urls);
        }

        public void AddUrlComplete(UrlProcessed url)
        {
            UrlComplete.Add(url);
        }

        public void ClearUrlComplete()
        {
            UrlComplete.Clear();
        }

        public void AddDirectoryListUrl(DirectoryList dir)
        {
            DirectoryListUrl.Add(dir);
        }

        public void ClearDirectoryListUrl()
        {
            DirectoryListUrl.Clear();
        }

        private void SetUrlTargets(List<UrlCheck> urls)
        {
            List<String> sList = new List<string>();

            foreach (UrlCheck url in urls)
            {
                sList.Add(url.Host);
            }

            UrlTargets = sList;
        }

        private void SetUrlTargets(String urls)
        {
            List<String> sList = new List<string>();
            string[] splitAry = urls.Split('\n');

            //remove whitespace and \r
            foreach (String s in splitAry)
            {
                String str = s.Trim();

                if (!String.IsNullOrEmpty(str))
                {
                    sList.Add(str);
                }
            }

            UrlTargets = sList;
        }

        public void SaveXml(Stream file)
        {
            try
            {
                XmlSerializer xml = new XmlSerializer(typeof(UrlSession));
                using (file)
                {
                    xml.Serialize(file, this);
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                throw;
            }
        }

        public static UrlSession LoadXml(Stream file)
        {
            try
            {
                UrlSession session = new UrlSession();

                XmlSerializer xml = new XmlSerializer(typeof(UrlSession));
                using (file)
                {
                     // Use the Deserialize method to restore the object's state.
                    session = (UrlSession)xml.Deserialize(file);
                }
                return session;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                throw;
            }

        }

    }
}
